//
//  LoginViewController.swift
//  MediatorPattern
//
//  Created by aron on 2017/6/20.
//  Copyright © 2017年 aron. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var accountTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var rememberAccountBtn: UIButton!
    @IBOutlet weak var autoLoginBtn: UIButton!
    @IBOutlet weak var loginBtn: UIButton!
    var cancelBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.cancelBtn = UIButton(type: .system);
        self.cancelBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 40)
        self.cancelBtn.setTitle("Cancel", for: .normal)
        self.cancelBtn.addTarget(self, action: #selector(LoginViewController.onCancelClick(sender:)), for: .touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: self.cancelBtn)
        
        NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.onTextFieldTextDidChanged(noti:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
        
        let password = UserDefaults.standard.object(forKey: "password") as? String
        let account = UserDefaults.standard.object(forKey: "account") as? String
        let isAutoLogin = UserDefaults.standard.object(forKey: "autoLogin") as? Bool
        let rememberAccount = UserDefaults.standard.object(forKey: "rememberAccount") as? Bool

        self.accountTextField.text = account
        self.passwordTextField.text = password
        self.onChanged(view: self.accountTextField)
        if rememberAccount != nil && rememberAccount! {
            self.onChanged(view: self.rememberAccountBtn)
        }
        if isAutoLogin != nil  && isAutoLogin!{
            self.onChanged(view: self.autoLoginBtn)
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func onCancelClick(sender: UIButton) {
        print("cancel")
        self.onChanged(view: sender)
    }
    
    @IBAction func onRemAccountBtnClick(_ sender: UIButton) {
        self.onChanged(view: sender)
    }
    
    @IBAction func onAutoLoginBtnClick(_ sender: UIButton) {
        self.onChanged(view: sender)
    }
    
    @IBAction func onLoginBtnClick(_ sender: UIButton) {
        self.onChanged(view: sender)
    }
    
    // MARK: - ......::::::: mediator :::::::......
    
    /// 处理View变化
    func onChanged(view: UIView) {
        if view == self.accountTextField {
            if (self.accountTextField.text?.characters.count)! > 0 {
                self.rememberAccountBtn.isEnabled = true
                self.autoLoginBtn.isEnabled = true
                
                if (self.passwordTextField.text?.characters.count)! > 0{
                    self.loginBtn.isEnabled = true
                } else {
                    self.loginBtn.isEnabled = false
                }
                
            } else {
                self.rememberAccountBtn.isSelected = false
                self.autoLoginBtn.isSelected = false
                self.rememberAccountBtn.isEnabled = false
                self.autoLoginBtn.isEnabled = false
                self.loginBtn.isEnabled = false
            }
        } else if view == self.passwordTextField {
            if (self.accountTextField.text?.characters.count)! > 0
            && (self.passwordTextField.text?.characters.count)! > 0{
                self.loginBtn.isEnabled = true
            } else {
                self.loginBtn.isEnabled = false
            }
        } else if view == self.rememberAccountBtn {
            self.rememberAccountBtn.isSelected = !self.rememberAccountBtn.isSelected
            if false == self.rememberAccountBtn.isSelected {
                self.autoLoginBtn.isSelected = false;
            }
        } else if view == self.autoLoginBtn {
            self.autoLoginBtn.isSelected = !self.autoLoginBtn.isSelected
            if self.autoLoginBtn.isSelected {
                self.rememberAccountBtn.isSelected = true
            }
        } else if view == self.loginBtn {
            print("handle login")
            self.storeLoginInfo()
        }  else if view == self.cancelBtn {
            print("cancel")
            self.storeLoginInfo()
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func storeLoginInfo() {
        if self.rememberAccountBtn.isSelected {
            UserDefaults.standard.set(true, forKey: "rememberAccount")
            UserDefaults.standard.setValue(self.passwordTextField.text, forKey: "password");
            UserDefaults.standard.setValue(self.accountTextField.text, forKey: "account");
            if self.autoLoginBtn.isSelected {
                UserDefaults.standard.set(true, forKey: "autoLogin")
            } else {
                UserDefaults.standard.set(false, forKey: "autoLogin")
            }
        } else {
            UserDefaults.standard.set(false, forKey: "rememberAccount")
            UserDefaults.standard.set(false, forKey: "autoLogin")
            UserDefaults.standard.setValue(nil, forKey: "password");
            UserDefaults.standard.setValue(nil, forKey: "account");
        }
        UserDefaults.standard.synchronize()
    }
    
    func onTextFieldTextDidChanged(noti: Notification) {
        let view = noti.object as! UIView
        self.onChanged(view: view)
    }

}
